import 'dart:async';
import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter_menu/app_config.dart';
import 'package:flutter_menu/biz/order/record/shop_car_controller.dart';
import 'package:flutter_menu/gen/assets.gen.dart';
import 'package:get/get.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:rxdart/rxdart.dart' as rxdart;

import '../../../data/menu_repository.dart';
import '../../../menu_page.dart';
import '../../../model/order_model.dart';
import '../../../res/app_design.dart';
import '../order_controller.dart';



class OrderRecordController extends GetxController {
  static int TAB_INDEX_ORDER_RECORD = 1;
  List<OrderRecordResp> myOrders  = [];
  final MenuRepository _repository = Get.find();
  final rxdart.CompositeSubscription _subs = rxdart.CompositeSubscription();
  StreamSubscription? _sub;
  late TabController tabController;

  @override
  void onInit() {
    super.onInit();

  }


  @override
  void onReady() {
    super.onReady();

  }


  void initRecords(){
    var id =  ShopCarController.to.tableId;
    if(id == null ){
      myOrders.clear() ;
      update();
    }else{
      _sub?.cancel();
       _sub =  _repository.getOrderRecordByTableId(id)
          .asStream()
          .listen((v){
        myOrders = v?? [];
        update();
      })..addTo(_subs);

    }
  }




  void onTabIndexChanged(int index){
    if(index == TAB_INDEX_ORDER_RECORD){
      initRecords();
    }

  }



  void continueOrder(){
    Get.toNamed(MenuPages.Menu_Order, parameters: {
      OrderController.orderPageStatusPara: OrderPageStatus.thumbnailPage.status
    });
  }

  @override
  void onClose() {
    super.onClose();
    _subs.clear();
  }


}


